//
//  LanguageViewModel.swift
//  Exchange
//
//  Created by rui on 2018/4/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

enum SettingType {
    case language
    case currency
}


struct SettingSelectViewModel {
    var dataArray:[SettingSelectModel] = [SettingSelectModel]()
    var type: SettingType
    
    init(type: SettingType) {
        self.type = type
        var fileName = ""
        if type == .language {
            fileName = "language"
        }else if type == .currency {
            fileName = "currency"
        }
        guard let path = Bundle.main.path(forResource: fileName, ofType: ".json") else {
            return
        }
        guard let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
            return
        }
        guard let dataArr = try? JSONDecoder().decode(Array<SettingSelectModel>.self, from: data) else {
            return
        }
    
        var name = ""
        
        if type == .language {
            name = AppCacheInfo.default.language
        }else if type == .currency {
            name = AppCacheInfo.default.currency
        }
        
        dataArray = dataArr.map { (model) -> SettingSelectModel in
            var m = model
            if m.name == name {
                m.isSelect = true
            }
            return m
        }
    }
    
    mutating func changeSetting(row:Int) {
        self.dataArray = self.dataArray.map { (model) -> SettingSelectModel in
            var m = model
            m.isSelect = false
            return m
        }
        var model = self.dataArray[row]
        model.isSelect = true
        self.dataArray.remove(at: row)
        self.dataArray.insert(model, at: row)
        
        if type == .language {
            let name = model.name ?? ""
            ///重新请求币对信息
            APPTransactionPair.default.isRequestIng = false
            APPTransactionPair.default.isRequestFail = true
            Bundle.setLanguage(name)
            AppCacheInfo.default.language = name
            let tab = BaseTabBarViewController()
            UIApplication.shared.keyWindow?.rootViewController = tab
            tab.selectedIndex = 3
            let nav = tab.selectedViewController as! NavigationController
            let sel = NSSelectorFromString("back")
            let setting = SettingViewController()
            let settingWrap = WrapViewController()
            settingWrap.contentViewController = setting
            settingWrap.hidesBottomBarWhenPushed = true
            setting.navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "nav_icon_back"), style: .plain, target: settingWrap.wrapNavigationController, action:sel)
            let language = LanguageOrCurrencyViewController()
            language.type = self.type
            let languageWrap = WrapViewController()
            languageWrap.contentViewController = language
            languageWrap.hidesBottomBarWhenPushed = true
            language.navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "nav_icon_back"), style: .plain, target: languageWrap.wrapNavigationController, action:sel)
            nav.pushViewController(settingWrap, animated: false)
            nav.pushViewController(languageWrap, animated: false)
            AppCacheInfo.default.language = name
        }else if type == .currency {
            SocketManager.shared.subscribe(server: ServerExRate())
            AppCacheInfo.default.currency = model.name!
        }
    }
  
}
